<template>
    <div class="">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" :model="queryParams" :inline="true">
                <el-form-item label="证书名称">
                    <el-input v-model="queryParams.name" placeholder="请输入证书名称" />
                </el-form-item>
                <el-form-item label="证书类型">
                    <el-select v-model="queryParams.type" placeholder="请选择证书类型">
                        <el-option label="白名单" :value="0" />
                        <el-option label="综合素质测评" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item label="证书分类">
                    <el-select v-model="queryParams.t_id" placeholder="请选择证书分类">
                        <el-option
                            v-for="item in types"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">搜索</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <router-link
                    v-perms="[
                        'certificate.certificate/certificateAdd/add',
                        'certificate.certificate/certificateAdd/add:edit'
                    ]"
                    :to="{
                        path: getRoutePath('certificate.certificate/certificateAdd/add:edit')
                    }"
                >
                    <el-button type="primary" class="mb-4">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        添加证书
                    </el-button>
                </router-link>
            </div>
            <el-table size="large" :data="pager.lists">
                <el-table-column label="ID" prop="id" min-width="80" />
                <el-table-column label="证书名称" prop="name" min-width="120" />
                <el-table-column label="证书图片" min-width="180">
                    <template #default="{ row }">
                        <el-image
                            v-if="row.images_url"
                            :src="row.images_url[0]"
                            style="width: 100px"
                            fit="contain"
                        />
                    </template>
                </el-table-column>

                <el-table-column label="证书类型" min-width="180">
                    <template #default="{ row }">
                        <el-tag type="success" v-if="row.type == 0">白名单</el-tag>
                        <el-tag type="primary" v-if="row.type == 1">综合素质测评</el-tag>
                    </template>
                </el-table-column>

                <el-table-column label="证书分类" prop="t_name" min-width="120" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="[
                                'certificate.certificate/certificateAdd/add',
                                'certificate.certificate/certificateAdd/add:edit'
                            ]"
                            type="primary"
                            link
                        >
                            <router-link
                                :to="{
                                    path: getRoutePath(
                                        'certificate.certificate/certificateAdd/add:edit'
                                    ),
                                    query: {
                                        id: row.id
                                    }
                                }"
                            >
                                编辑
                            </router-link>
                        </el-button>
                        <el-button type="danger" link @click="handleDelete(row.id)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>
<script lang="ts" setup name="articleLists">
import { usePaging } from '@/hooks/usePaging'
import { certificateList, deleteCertificate, classification } from '@/api/certificate'
import { getRoutePath } from '@/router'
import feedback from '@/utils/feedback'
const queryParams = reactive({
    name: '',
    type: '',
    t_id: ''
})
const types = ref([])

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: certificateList,
    params: queryParams
})

// 获取分类
const getbannerinfo = async () => {
    const data = await classification({})
    types.value = data
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await deleteCertificate({ id })
    getLists()
}

getLists()
getbannerinfo()
</script>
